#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif
//#include <commdlg.h>
#include "CCanvas.h"
CCanvas *lpCanvas;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
    HMENU hMenu;

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon(hThisInstance, MAKEINTRESOURCE(MAIN_ICON)); //(NULL, MAIN_ICON);
    wincl.hIconSm = wincl.hIcon;
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    CCanvas canvas;
    lpCanvas = &canvas;
    hMenu = LoadMenu(hThisInstance, MAKEINTRESOURCE(MAIN_MENU));

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Hanoi"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           1000,                 /* The programs width */
           600,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           hMenu,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    char cKeyCode, *lpstrIntroduction;
    HINSTANCE hInstance;
    static HMENU hMenu = GetMenu(hwnd);
    HDC hdc;
    PAINTSTRUCT ps;
    OPENFILENAME ofn;
    char lpstrFile[256];
    HDC hMetaFileDC;
    int i, iChange;
    switch (message)                  /* handle the messages */
    {
    case WM_COMMAND:
        switch (wParam)
        {
        case ID_EXPORT_EMF:
            {
                memset(lpstrFile, 0, 256 * sizeof(char));
                memset(&ofn, 0, sizeof(ofn));
                ofn.lStructSize = sizeof(ofn);
                ofn.hwndOwner = hwnd;
                ofn.lpstrFile = lpstrFile;
                ofn.Flags = OFN_OVERWRITEPROMPT;
                ofn.nMaxFile = 255;
                ofn.lpstrFilter = "Enhanced Meta Files(*.emf)\0*.emf\0All Files(*.*)\0*.*\0";
                ofn.nFilterIndex = 1;
                if (GetSaveFileName(&ofn))
                {
                    hMetaFileDC = CreateEnhMetaFile(NULL, lpstrFile, NULL, NULL);
                    lpCanvas->OnPaint(hwnd, hMetaFileDC);
                    CloseEnhMetaFile(hMetaFileDC);
                }
            }
            break;
        case ID_EXIT:
            PostQuitMessage(0);
            break;
        case ID_LAYER_ONE:
        case ID_LAYER_TWO:
        case ID_LAYER_THREE:
        case ID_LAYER_FOUR:
        case ID_LAYER_FIVE:
        case ID_LAYER_SIX:
        case ID_LAYER_SEVEN:
            iChange = lpCanvas->OnKeyUp(char('1' + (wParam - ID_LAYER_ONE)), hwnd);
            if (iChange > 0 && iChange <= 7)
            {
                for (i = 0; i < 7; ++i)
                {
                    if (i + 1 == iChange)
                    {
                        CheckMenuItem(hMenu, ID_LAYER_ONE + i, MF_CHECKED);
                    }
                    else
                    {
                        CheckMenuItem(hMenu, ID_LAYER_ONE + i, MF_UNCHECKED);
                    }
                }
            }
            break;
        case ID_SOLVE:
            lpCanvas->OnKeyUp('A', hwnd);
            break;
        case ID_FORWARD:
            lpCanvas->OnKeyUp('F', hwnd);
            break;
        case ID_BACKWARD:
            lpCanvas->OnKeyUp('B', hwnd);
            break;
        case ID_RECTANGLE:
            lpCanvas->iPlateShape = ID_RECTANGLE;
            CheckMenuItem(hMenu, ID_RECTANGLE, MF_CHECKED);
            CheckMenuItem(hMenu, ID_ROUND, MF_UNCHECKED);
            InvalidateRect(hwnd, NULL, TRUE);
            break;
        case ID_ROUND:
            lpCanvas->iPlateShape = ID_ROUND;
            CheckMenuItem(hMenu, ID_RECTANGLE, MF_UNCHECKED);
            CheckMenuItem(hMenu, ID_ROUND, MF_CHECKED);
            InvalidateRect(hwnd, NULL, TRUE);
            break;
        case ID_INTRODUCTION:
            hInstance = GetModuleHandle(NULL);
            lpstrIntroduction = new char[4096];
            LoadString(hInstance, STR_INTRODUCTION, lpstrIntroduction, 4095);
            MessageBox(hwnd, _T(lpstrIntroduction), _T("Introduction"), MB_OK);
            break;
        }
        break;
    case WM_KEYUP:
        cKeyCode = wParam;
        if (cKeyCode == 's' || cKeyCode == 'S')
        {
            memset(lpstrFile, 0, 256 * sizeof(char));
            memset(&ofn, 0, sizeof(ofn));
            ofn.lStructSize = sizeof(ofn);
            ofn.hwndOwner = hwnd;
            ofn.lpstrFile = lpstrFile;
            ofn.Flags = OFN_OVERWRITEPROMPT;
            ofn.nMaxFile = 255;
            ofn.lpstrFilter = "Enhanced Meta Files(*.emf)\0*.emf\0All Files(*.*)\0*.*\0";
            ofn.nFilterIndex = 1;
            if (GetSaveFileName(&ofn))
            {
                hMetaFileDC = CreateEnhMetaFile(NULL, lpstrFile, NULL, NULL);
                lpCanvas->OnPaint(hwnd, hMetaFileDC);
                CloseEnhMetaFile(hMetaFileDC);
            }
        }
        else
        {
            iChange = lpCanvas->OnKeyUp(cKeyCode, hwnd);
            if (iChange > 0 && iChange <= 7)
            {
                for (i = 0; i < 7; ++i)
                {
                    if (i + 1 == iChange)
                    {
                        CheckMenuItem(hMenu, ID_LAYER_ONE + i, MF_CHECKED);
                    }
                    else
                    {
                        CheckMenuItem(hMenu, ID_LAYER_ONE + i, MF_UNCHECKED);
                    }
                }
            }
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        lpCanvas->OnPaint(hwnd, hdc);
        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
